package org.cttic.visualization.visualization.z_service.first;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import org.cttic.visualization.visualization.common.TravelTransRequest;
import org.cttic.visualization.visualization.common.TravelTransResponse;
import org.cttic.visualization.visualization.common.module.provinceData;
import org.cttic.visualization.visualization.util.CreatVehicle;
import org.cttic.visualization.visualization.util.DateUtil;
import org.cttic.visualization.visualization.util.ResponseObject;
import org.cttic.visualization.visualization.z_service.TransProvincialStatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.security.SecureRandom;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;

@Service
public class TransProvincialStatisticsServiceImpl implements TransProvincialStatisticsService {


    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public ResponseObject statisticsTrans(JSONObject json) {

        String provinceId = json.getStr("provinceId");

        String queryType = json.getStr("queryType");


        String sql = "SELECT provinceId,provinceName,enCount vehInCount,exCount vehOutCount from trans_provincial_statistics";

        if (StrUtil.isNotEmpty(provinceId)) {
            String[] split = provinceId.split("\\|");
            String s1 = "";
            for (String s : split) {
                s1 += "'" + s + "',";
            }
            String substring = s1.substring(0, s1.length() - 1);

            sql += " where provinceId in (" + substring + ") ";
        }

        List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);
        JSONArray array = new JSONArray();

        for (Map<String, Object> map : maps) {
            JSONObject object = new JSONObject(map);
            int vehInCount = object.getInt("vehInCount").intValue();

            int vehOutCount = object.getInt("vehOutCount").intValue();
            int totalCount = vehInCount + vehOutCount;

            object.put("totalCount", totalCount);
            array.add(object);
        }
        return ResponseObject.success(array);
    }


    public ResponseObject statisticsVehicle(JSONObject json) {
        String provinceId = json.getStr("provinceId");

        String direction = json.getStr("direction");

        String sql = null;
        if ("1".equals(direction)) {

            sql = " SELECT provinceId,enCount inNums,0 outNums from trans_provincial_statistics  ";
        } else if ("2".equals(direction)) {
            sql = " SELECT provinceId,0 inNums,exCount outNums from trans_provincial_statistics  ";
        } else {
            sql = " SELECT provinceId,enCount inNums,exCount outNums from trans_provincial_statistics ";
        }
        if (StrUtil.isNotEmpty(provinceId)) {
            String[] split = provinceId.split("\\|");
            String s1 = "";
            for (String s : split) {
                s1 += "'" + s + "',";
            }
            String substring = s1.substring(0, s1.length() - 1);

            sql += " where provinceId in (" + substring + ") ";
        }

        List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);


        JSONArray array = new JSONArray(maps);

        return ResponseObject.success(array);

    }

    @Override
    public ResponseObject statisticsTravel(TravelTransRequest request) {

        String provinceId = request.getProvinceId();
        String queryValue1 = request.getQueryValue1();
        String queryValue2 = request.getQueryValue2();

        int pageNo = request.getPageNo().intValue();
        int pageSize = request.getPageSize().intValue();


        if (StrUtil.isEmpty(queryValue1)) {
            return ResponseObject.error("queryValue1不能为空");
        }
        if (StrUtil.isEmpty(queryValue2)) {
            return ResponseObject.error("queryValue2不能为空");
        }

        TravelTransResponse response = new TravelTransResponse();

        String proWhere = " ";

        if (StrUtil.isNotEmpty(provinceId)) {
            proWhere = " provinceId in (";
            String[] split = provinceId.split("\\|");
            String s = "";
            for (String s1 : split) {
                s += "'" + s1 + "',";
            }
            String substring = s.substring(0, s.length() - 1);
            proWhere += (substring + ") and ");
        }


        String countSql = " select count(distinct gantryId) total from travel_trans_statics where "
            + proWhere
            + " ( hour between '" + queryValue1 + "' and '" + queryValue2 +
            "' ) ";

        Integer total = jdbcTemplate.queryForObject(countSql, Integer.class);

        if (total == 0) return ResponseObject.success(response);

        int totalPage = total / pageSize;

        if (total % pageSize != 0)
            totalPage++;

        int start = (pageNo - 1) * pageSize;

        String sql = "SELECT sum(TOTAL) total_1,gantryId,gantryName,provinceId from travel_trans_statics where"
            + proWhere
            + " ( hour between '" + queryValue1 + "' and '" + queryValue2 +
            "' ) group by gantryId,gantryName,provinceId " +
            " order by total_1 desc  limit "+start+"," + pageSize;

        List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);

        List<JSONObject>  vehTrack = new ArrayList<>();

        for (Map<String, Object> map : maps) {
            JSONObject object = new JSONObject(map);
            String provinceName = provinceData.provinceID.get(object.getInt("provinceId"));
            object.put("provinceName", provinceName);
            object.put("time", queryValue1 + "-" + queryValue2);
            object.put("total", object.getStr("total_1"));
            object.remove("total_1");
            vehTrack.add(object);

        }

        response.setPageNo(pageNo);
        response.setPageSize(pageSize);
        response.setTotalCount(total);
        response.setTotalPage(totalPage);
        response.setVehTrack(vehTrack);


        return ResponseObject.success(response);
    }

}
